/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
function showCategoryManagementPanel() {
    categoryManagement = $("#categoryManagement");
    
    windowHeight = $(window).height();
    windowWidth = $(window).width();
    popupTop = (windowHeight - 300) / 2;
    popupLeft = (windowWidth - 400) / 2;

    categoryManagement.css("top", popupTop);
    categoryManagement.css("left", popupLeft);

    $.ajax({
        type: "GET",
        url: "/public/blog/getAllCategories",
        data: {
            "task": "getAllCategories"
        },
        dataType: "json",
        success: function(json){
            updateCategoryList(json);
            categoryManagement.show(100);
        }
    })
}

function closePanel() {
    categoryManagement = $("#categoryManagement");
    categoryManagement.hide(100);
}

function initCategory() {
    $.ajax({
        type: "GET",
        url: "/public/blog/getAllCategories",
        data:{
            "task": "getAllCategories"
        },
        dataType: "json",
        success: function(json){
            initCategoryList(json);
        }
    })
}

function addNewCategory() {
    newCategory = $("#createCategory>input[type='text']").val();
    state = $("#createCategory>em");

    if(newCategory.length<1 || newCategory == null){
        state.text("Category must not be null or empty!");
        state.fadeIn(1000, function(){
            state.fadeOut(5*1000);
        })
    }else{
        $.ajax({
            type: "POST",
            url: "/admin/blog/addCategory",
            data:{
                "task": "createCategory",
                "category": newCategory
            },
            dataType: "json",
            success: function(json){
                if(json.exist) {
                    state.text("This category have been ceated!");
                } else {
                    state.text("Category was added successfully!");
                    updateCategoryList(json);
                }
                state.fadeIn(1000, function(){
                    state.fadeOut(5*1000);
                });
            }
        })
    }
}

function deleteCategory(nickName) {
    state = $("#createCategory>em");

    $.ajax({
        type: "GET",
        url: "/admin/blog/deleteCategory",
        data:{
            "task": "deleteCategory",
            "nickName": nickName
        },
        dataType: "json",
        success: function(json){
            state.text("Category was deleted successfully!");
            state.fadeIn(1000, function(){
                state.fadeOut(5*1000);
            });
            updateCategoryList(json);
        }
    })
}

function initCategoryList(json) {
    blogCategory = $("select[name='blogCategory']");
    blogCategory.html("");

    if(json.categories.length != 0) {
        option = $("<option value='-1'>Please chose</option>");
        option.appendTo(blogCategory);
        for(index in json.categories) {
            category = json.categories[index];
            option = $("<option value='"+category.nickName+"'>"+category.name+"</option>");
            option.appendTo(blogCategory);
        }
    }

}

function updateCategoryList(json) {
    haveCreated = $("#haveCreated");
    haveCreated.html("");

    ul = $("<ul>");
    for(index in json.categories){
        category = json.categories[index];
        li = $("<li>"+
            "<label>"+category.name+"</label>"+
            "<div class='deleteCategory' onclick=\"deleteCategory('"+category.nickName+"')\"></div>"+
            "</li>");
        li.appendTo(ul);
    }
    ul.appendTo(haveCreated);
}

function deleteBlog(id) {

    confirmDelete = confirm("Are you sure you want to delete?");
    if(confirmDelete){
        $.ajax({
            type: "post",
            url: "/admin/blog/deleteBlog",
            data:{
                "task": "deleteBlog",
                "id": id
            },
            success: function(){
                window.open("/queryBlog.do?scope=all&page=1","_self");
            }
        })
    }
}


